import request from '@/utils/request'
import type {
  UserListResponseData,
  UserListData,
  UserAndAllRoleResponseData,
  setUserRoleData,
} from './type'

const API = {
  USERINFOBYPAGE_URL: '/admin/acl/user/',
  SAVEUSERINFO_URL: '/admin/acl/user/save',
  UPDATEUSERINFO_URL: '/admin/acl/user/update',
  USERANDALLROLES_URL: '/admin/acl/user/toAssign/',
  SETUSERROLE_URL: '/admin/acl/user/doAssignRole',
  DELETEUSER_URL: '/admin/acl/user/remove/',
  DELETEUSERS_URL: '/admin/acl/user/batchRemove',
}

export const reqUserInfoByPage = async (
  page: number,
  limit: number,
  username: string,
) => {
  let suffix = username
    ? `${page}/${limit}?username=${username}`
    : `${page}/${limit}`
  return await request.get<any, UserListResponseData>(
    API.USERINFOBYPAGE_URL + suffix,
  )
}

export const reqSaveOrUpdateUserInfo = async (data: UserListData) => {
  if (data.id) {
    //修改
    return await request.put<any, any>(API.UPDATEUSERINFO_URL, data)
  } else {
    //新增
    return await request.post<any, any>(API.SAVEUSERINFO_URL, data)
  }
}

export const reqUserAndAllRoles = async (userId: number) =>
  await request.get<any, UserAndAllRoleResponseData>(
    API.USERANDALLROLES_URL + userId,
  )

export const reqSetUserRole = async (data: setUserRoleData) =>
  await request.post<any, any>(API.SETUSERROLE_URL, data)

export const reqDeleteUser = async (userId: number) =>
  await request.delete<any, any>(API.DELETEUSER_URL + userId)

export const reqDeleteSelectedUsers = async (idList: number[]) =>
  await request.delete<any, any>(API.DELETEUSERS_URL, { data: idList })
